package com.google.gwt.sample.dynatablerf.server.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import com.google.gwt.sample.dynatablerf.server.business.ControladorUsuario;

public class CustomAuthenticationProvider implements AuthenticationProvider {

	@Autowired
	private ControladorUsuario controladorUsuario;

	@Override
	public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {

		String username = (String) authentication.getPrincipal();
		String password = (String) authentication.getCredentials();

		controladorUsuario.validarUsuario(username, password);

		Authentication customAuthentication = new CustomUserAuthentication(
				"ROLE_USER", authentication);
		customAuthentication.setAuthenticated(true);

		return customAuthentication;

	}

	@Override
	public boolean supports(Class<? extends Object> authentication) {
		return UsernamePasswordAuthenticationToken.class
				.isAssignableFrom(authentication);
	}

}